package com.tsingteng.cosfun.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tsingteng.cosfun.R;

import java.util.Random;

/**
 * 描述 :动画工具类
 * 创建人: 肖利.
 * 时间:2018/8/27
 * qq(872179966)
 */

public class AnimatorUtils {
    private static  AnimatorUtils animatorUtils;
    private static ObjectAnimator scaleBig;

    private AnimatorUtils() {
    }
    public static final AnimatorUtils getIntecnce() {
        if (animatorUtils==null) {
            synchronized (AnimatorUtils.class) {
                if (animatorUtils==null) {}
                animatorUtils = new AnimatorUtils();
            }
        }
        return animatorUtils;
    }


    /**
     * 旋转动画
     * @param view
     * @param time
     * @param values
     * @return
     */
    public  void rotation(View view, long time, float fromValue,float values) {

        Animation anim =new RotateAnimation(fromValue, values, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setFillAfter(true); // 设置保持动画最后的状态
        anim.setDuration(time); // 设置动画时间
        anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
        view.startAnimation(anim);
    }

    /**
     * 点赞的动画
     * @param view
     * @param touchX
     * @param touchY
     */
    public  void doublePraseAnimtor(ViewGroup view, int touchX, int touchY) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(260,260);
        params.topMargin = touchY-130;
        params.leftMargin = touchX-130;
        ImageView imageView = new ImageView(view.getContext());
        imageView.setImageResource(R.drawable.yp_video_like);
        imageView.setLayoutParams(params);
        Random random = new Random();
        int num = random.nextInt(60)-30;
        imageView.setRotation(num);
        view.addView(imageView);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))//缩放动画，X轴2倍缩小至0.9倍
                .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))//缩放动画，Y轴2倍缩小至0.9倍
                .with(rotationView(imageView, 0, 0, num))//旋转动画，随机旋转角度num={-30.-20，0，20，30}
                .with(alpha(imageView, 0, 1, 100, 0))//渐变透明度动画，透明度从0-1.
                .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))//缩放动画，X轴0.9倍缩小至1倍
                .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))//缩放动画，Y轴0.9倍缩小至1倍
                .with(translationY(imageView, 0, -800, 800, 400))//平移动画，Y轴从0向上移动600单位
                .with(alpha(imageView, 1, 0, 300, 400))//透明度动画，从1-0
                .with(scale(imageView, "scaleX", 1, 3f, 700, 400))//缩放动画，X轴1倍放大至3倍
                .with(scale(imageView, "scaleY", 1, 3f, 700, 400));//缩放动画，Y轴1倍放大至3倍
        animatorSet.start();
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.removeViewInLayout(imageView);
            }
        });
    }

    public  ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    public  ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    public  ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }
    public  ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public  ObjectAnimator rotationView(View view, long time, long delayTime, float... values) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        return rotation;
    }
}
